/*******************************************************************************
 * Copyright (c) 2013,  Paul Daniels
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.hupu.jrs.components.jockeyjs

import android.content.Context
import android.os.Vibrator
import android.widget.Toast

/**
 * <blockquote>Provides helper methods to easily create handlers that implement
 * system services
</blockquote> *
 * <br></br>
 *
 *
 * `
 * //Creates a handler that will generate a toast when invoked <br></br>
 * nativeOS(context).toast("A message", Toast.LENGTH_SHORT); <br></br>
 * <br></br>
 * //Creates a handler that operates the OS's haptic feed back <br></br>
 * nativeOS(context).vibrate(45); <br></br>
` *
 *
 *
 *
 * @author Paul
 */
class NativeOS private constructor(private val _context: Context) : CompositeJockeyHandler() {

    class ToastHandler (private val _context: Context, private val _message: String, private val _length: Int) : JockeyHandler() {

        override fun doPerform(payload: Map<Any, Any>) {
            Toast.makeText(_context, _message, _length).show()
        }

        override fun completed(listener: JockeyHandler.OnCompletedListener?) {
            listener?.onCompleted()
        }
    }

    class VibrateHandler (private val _context: Context, private val _length: Long) : JockeyHandler() {
        private val _vibrator: Vibrator

        init {
            _vibrator = _context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        }

        override fun doPerform(payload: Map<Any, Any>) {
            _vibrator.vibrate(_length)
        }

        override fun completed(listener: JockeyHandler.OnCompletedListener?) {
            listener?.onCompleted()
        }
    }

    fun vibrate(length: Long): NativeOS {
        this.add(VibrateHandler(_context, length))
        return this
    }

    fun toast(message: String, length: Int): NativeOS {
        this.add(ToastHandler(_context, message, length))
        return this
    }

    companion object {

        fun nativeOS(context: Context): NativeOS {
            return NativeOS(context)
        }
    }
}
